<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button v-if="hasPerm('Quot_OfferSheetMain1.Add')" icon="plus" type="primary" @click="handleAdd()">新建</a-button>
      <a-button
        v-if="hasPerm('Quot_OfferSheetMain1.Delete')"
        :disabled="!hasSelected()"
        :loading="loading"
        icon="minus"
        type="primary"
        @click="handleDelete(selectedRowKeys)"
      >删除
      </a-button>
      <a-button icon="redo" type="primary" @click="getDataList()">刷新</a-button>
    </div>
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select v-model="queryParam.condition" allowClear>
                <a-select-option key="DocumentNo">报价单编号</a-select-option>
                <a-select-option key="CustomerName">客户名称</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword" placeholder="关键字" @keydown.enter="getDataList"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              ">查询
            </a-button>
            <a-button style="margin-left: 8px" @click="repossess">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-table
      ref="table"
      :bordered="true"
      :columns="columns"
      :dataSource="data"
      :loading="loading"
      :pagination="pagination"
      :rowKey="(row) => row.Id"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, getCheckboxProps: CheckboxProps }"
      :scroll="{ y: 530 }"
      size="small"
      @change="handleTableChange">
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <span slot="Discount" slot-scope="Discount" v-format="'0%'">{{ Discount / 100 }}</span>
      <span slot="TotalAmount" slot-scope="TotalAmount" v-format="'#,##0.00'">{{ TotalAmount }}</span>
      <span slot="Taxes" slot-scope="Taxes" v-format="'#,##0.00'">{{ Taxes }}</span>
      <span slot="Freight" slot-scope="Freight" v-format="'#,##0.00'">{{ Freight }}</span>
      <template slot="ApprovalStatus" slot-scope="text, record, index">
        <a-tag v-if="text == 1" color="green"> 审批通过</a-tag>
        <a-tag v-if="!text" color="blue"> 未审批</a-tag>
        <a-tag v-if="text == 2" color="red"> 审批驳回</a-tag>
      </template>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="record.ApprovalStatus == 1 && hasPerm('Quot_OfferSheetMain1.Print')" @click="Print(record.Id)">打印</a>
          <a-divider v-if="record.ApprovalStatus == 1" type="vertical"/>
          <a v-if="hasPerm('Quot_OfferSheetMain1.Edit') && !record.ApprovalStatus" @click="handleEdit(record.Id)">编辑</a>
          <a-divider v-if="hasPerm('Quot_OfferSheetMain1.Edit') && hasPerm('Quot_OfferSheetMain1.Delete') && !record.ApprovalStatus " type="vertical"/>
          <a v-if="record.ApprovalStatus" @click="Check(record.Id)">查看</a>
          <a-divider v-if="record.ApprovalStatus == 2" type="vertical"/>
          <a v-if="record.ApprovalStatus == 2" @click="handleRevise(record.Id)">修改</a>
          <a v-if="hasPerm('Quot_OfferSheetMain1.Delete') && !record.ApprovalStatus" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>
    <edit-form ref="editForm" :disabled="disabled" :parentObj="this"></edit-form>
    <!-- 打印 -->
    <Print ref="Print" :parentObj="this"></Print>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
import OperatorCache from '@/utils/cache/OperatorCache'
// 打印
import Print from './Print.vue'
import moment from 'moment'

const columns = [
  { title: '报价单编号', dataIndex: 'DocumentNo', width: '9%' },
  { title: '客户名称', dataIndex: 'CustomerName', width: '16%' },
  { title: '销售经理', dataIndex: 'AccountManager', width: '8%' },
  // { title: '交易类型', dataIndex: 'TransactionType' },
  // { title: '开票类型', dataIndex: 'BillingType' },
  { title: '折扣%', dataIndex: 'Discount', align: 'center', width: '80px', scopedSlots: { customRender: 'Discount' } },
  { title: '金额合计(元)', dataIndex: 'TotalAmount', align: 'right', width: '120px', scopedSlots: { customRender: 'TotalAmount' } },
  { title: '税金(元)', dataIndex: 'Taxes', align: 'right', width: '120px', scopedSlots: { customRender: 'Taxes' } },
  // { title: '运输方式', dataIndex: 'TransportType' },
  { title: '送货地址', dataIndex: 'DeliveryAddress', width: '17%', ellipsis: true },
  { title: '运费(元)', dataIndex: 'Freight', align: 'right', width: '100px', scopedSlots: { customRender: 'Freight' } },
  { title: '联系人', dataIndex: 'LinkMan', width: '100px' },
  { title: '审批状态', dataIndex: 'ApprovalStatus', align: 'center', scopedSlots: { customRender: 'ApprovalStatus' }, width: '100px' },
  // { title: '备注', dataIndex: 'Remark' },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' }, width: '10%' }
]

export default {
  components: {
    EditForm,
    Print,
  },
  // activated() {
  //   console.log(this.$route.query.name)
  //   if (this.$route.query.name) {
  //     this.queryParam.condition = 'CustomerName'
  //     this.queryParam.keyword = this.$route.query.name
  //     this.getDataList()
  //   } else {
  //     this.queryParam.condition = 'CustomerName'
  //     this.queryParam.keyword = ''
  //     this.getDataList()
  //   }
  // },
  created () {
    this.getDataList()
  },
  data () {
    return {
      disabled: false,
      data: [],
      Id: '', //当前登录人Id
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true,
      },
      filters: {},
      CreatorId: this.Id,
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: {
        condition: 'CustomerName',
      },
      selectedRowKeys: [],
    }
  },
  methods: {
    CheckboxProps (record) {
      return {
        props: {
          disabled: record.ApprovalStatus == 1,
        },
      }
    },
    handleRevise (id) {
      this.disabled = false
      this.$refs.editForm.openForm(id, '修改')
    },
    repossess () {
      this.queryParam = { condition: 'CustomerName' }

    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    op () {
      return OperatorCache.info
    },
    getDataList () {
      this.selectedRowKeys = []
      this.Id = this.op().Id
      this.loading = true
      this.$http
        .post('/QuotationManagement/Quot_OfferSheetMain/GetDataListByMySelfAndMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: {
            Condition: this.queryParam.condition,

            Keyword: this.queryParam.keyword,
          },
        })
        .then((resJson) => {
          // //console.log(this.op())
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    Print (id) {
      this.$refs.Print.openForm(id, '打印')
    },
    onSelectChange (selectedRowKeys) {
      //console.log(selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.disabled = false
      this.$refs.editForm.openForm(null, '新建我的报价信息')
    },
    handleEdit (id) {
      this.disabled = false
      this.$refs.editForm.openForm(id, '编辑我的报价信息')
    },
    Check (id) {
      this.disabled = true
      this.$refs.editForm.openForm(id, '查看')
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/QuotationManagement/Quot_OfferSheetMain/DeleteDataOfAll', ids).then((resJson) => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        },
      })
    },
    timeFormat (val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    },
  },
}
</script>
